﻿using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace August
{
    public partial class Form1 : Form
    {
        Mat mat;
        AutoResetEvent autoResetEvent = new AutoResetEvent(false);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap formBitmap = new Bitmap(operationPanel.shapeContainer.Width, operationPanel.shapeContainer.Height);
            operationPanel.shapeContainer.DrawToBitmap(formBitmap, new Rectangle(0, 0, operationPanel.shapeContainer.Width, operationPanel.shapeContainer.Height));
            formBitmap.Save("f:\\kankan\\form.png", ImageFormat.Png);
            //pictureBox1.Image = drawPanel1.circleXLD.tBmp;
            //drawPanel1.circleXLD.tBmp.Save("f:\\kankan\\ee.png", System.Drawing.Imaging.ImageFormat.Png);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string[] file = null;
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                file = fileDialog.FileNames;
            }
            else
            {
                return;
            }
            if (file == null && file.Length == 0) return;
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < file.Length; i++)
                {
                    CV.CV_Imread(file[i], out mat, Emgu.CV.CvEnum.ImreadModes.AnyColor);
                    Mat matThreshold = new Mat();
                    CV.CV_Threshold(mat, 0, 255, Emgu.CV.CvEnum.ThresholdType.Otsu, ref matThreshold);
                    operationPanel.Dev_DispMat(matThreshold);
                    //autoResetEvent.WaitOne();
                    Mat matBitWiseNot = new Mat();
                    CV.CV_BitwiseNot(matThreshold, ref matBitWiseNot);
                    Mat matErode = new Mat();
                    CV.CV_Erode(matBitWiseNot, 3, ref matErode);
                    VectorOfVectorOfPoint vectorOfVectorOfPoint;
                    VectorOfPoint vectorOfPoint;
                    Mat matFindRegion = new Mat();
                    CV.CV_FindRegion(matErode, out vectorOfVectorOfPoint, out vectorOfPoint, ref matFindRegion);
                    CircleF circleF;
                    Mat matMinEnclosingCircle = new Mat();
                    CV.CV_MinEnclosingCircle(vectorOfPoint, out circleF);
                    Circle circle;
                    CV.GenCircleXld(circleF.Center.X, circleF.Center.Y, circleF.Radius, out circle);
                    Line line;
                    CV.GenLineXld(new PointF[3] { new PointF(0, 0), new PointF(100, 100), new PointF(200, 100) },out line);
                    operationPanel.Dev_DispClear();
                    operationPanel.Dev_DispMat(mat);
                    operationPanel.Dev_SetColor(Color.Red);
                    operationPanel.Dev_DispXld(circle);
                    operationPanel.Dev_SetColor(Color.Green);
                    operationPanel.Dev_DispXld(line);
                    autoResetEvent.WaitOne();
                }
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            autoResetEvent.Set();
        }
    }
}
